package com.hnqcgc.sgzShopping.db;
import java.sql.Connection;
import java.sql.SQLException;
public class MyThansaction {
    private boolean isAuto;
    Connection conn;
    public MyThansaction(boolean isAuto){
        try {
          // conn = new DbImpl().getConn(); //拿到数据库连接
            IDb db=new DbImpl();//多态
            Connection conn = db.getConn();
            //设置提交方式
            this.conn.setAutoCommit(isAuto);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public void commit(){ //手动提交的方法
        try {
            if(!conn.isClosed()&&conn!=null){
                conn.commit();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public void rollback(){ //手动回滚的方法
        try {
            if(!conn.isClosed()&&conn!=null){
                conn.rollback();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //返回这个新的数据库连接对象
    public Connection getConnction()throws SQLException{
        return conn;
    }
    //关闭数据库连接

    public void closeDatabase(){
        //数据库连接是打开状态  或者 对象不为null
        try {
            if(conn!=null&&!conn.isClosed()){
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }


}
